// SplashScreen.java
package sun.scheduler.gui;

// Eventuell hier erst das Paket definieren, falls erforderlich! 
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.net.URL;

public class SplashScreen extends Window {

    private Image m_imageSplash = null;
    private MediaTracker m_mediaTracker = null;
    private float alpha = 0.0f;

    public SplashScreen(String strImagePath, Dimension dimImageSize) {
        super(new Frame());
        setLocation((getToolkit().getScreenSize().width - dimImageSize.width) / 2,
                (getToolkit().getScreenSize().height - dimImageSize.height) / 2);
        setSize(dimImageSize);

        if (strImagePath != null && dimImageSize != null) {
            URL url = getClass().getResource(strImagePath);
            m_imageSplash = getToolkit().getImage(url);
            m_mediaTracker = new MediaTracker(this);
            m_mediaTracker.addImage(m_imageSplash, 0);

            try {
                m_mediaTracker.waitForAll();
            } catch (InterruptedException e) {
            }
        }
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        //set the opacity
        g2d.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha));
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //draw IMAGE
        g2d.drawImage(m_imageSplash, 0, 0, getSize().width, getSize().height, this);
        //increase the opacity and repaint
        alpha += 0.05f;
        if (alpha >= 1.0f) {
            alpha = 1.0f;

        } else {
            repaint();
        }


        //sleep for a bit
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    }

    public void start() {
        setVisible(true);
        Sound s = new Sound("media/sound/startup2.wav");
        s.playSoundOnce();
    }

    public void destroy() {
        setVisible(false);
        dispose();
    }

    public class Sound // Holds one audio file
    {

        private AudioClip song; // Sound player
        private URL songPath; // Sound path

        Sound(String filename) {
            try {
                songPath = getClass().getResource(filename);
                song = Applet.newAudioClip(songPath); // Load the Sound
            } catch (Exception e) {
            } // Satisfy the catch
        }

        public void playSound() {
            song.loop(); // Play 
        }

        public void stopSound() {
            song.stop(); // Stop
        }

        public void playSoundOnce() {
            song.play(); // Play only once
        }
    }
}